package com.yanqu.road.utils.net;

import com.yanqu.road.utils.ServerType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;

/**
 * 网络配置
 */
public class ServerNetConfigHelper {

    private static Logger logger = LogManager.getLogger(ServerNetConfigHelper.class.getName());

    private static int agentCount = 0;

    private static Map<Integer, ServerNetConfig> agentMap = new HashMap<>();
    private static Map<Integer, ServerNetConfig> gameMap = new HashMap<>();
    private static Map<Integer, ServerNetConfig> webMap = new HashMap<>();

    public static boolean init(Map<String, List<ServerNetConfig>> netConfigMap) {
        List<ServerNetConfig> agentConfigList = netConfigMap.get("agent_server");
        if(null == agentConfigList || agentConfigList.size() <= 0){
            return false;
        }
        List<ServerNetConfig> webConfigList = netConfigMap.get("web_server");
        if(null == webConfigList || webConfigList.size() <= 0){
            return false;
        }
        List<ServerNetConfig> gameConfigList = netConfigMap.get("game_server");
        if(null == gameConfigList || gameConfigList.size() <= 0){
            return false;
        }

        for (ServerNetConfig netConfig : agentConfigList) {
            agentMap.put(netConfig.getServerIndex(), netConfig);
        }

        for (ServerNetConfig netConfig : gameConfigList) {
            gameMap.put(netConfig.getServerIndex(), netConfig);
        }

        for (ServerNetConfig netConfig : webConfigList) {
            webMap.put(netConfig.getServerIndex(), netConfig);
        }
        agentCount = agentMap.size();
        return true;
    }

    /**
     * 服务器配置
     * @param serverType
     * @param serverIndex
     * @return
     */
    public static ServerNetConfig getServerNetConfig(int serverType, int serverIndex) {
        ServerNetConfig netConfig = null;
        switch (serverType) {
            case ServerType.SERVER_AGENT:
                netConfig = agentMap.get(serverIndex);
                break;
            case ServerType.SERVER_GAME:
                netConfig = gameMap.get(serverIndex);
                break;
            case ServerType.SERVER_WEB:
                netConfig = webMap.get(serverIndex);
                break;
        }
        return netConfig;
    }

    /**
     * 网关连接数量
     */
    public static int getAgentCount() {
        return agentCount;
    }

}
